#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
const int N = 4e6 + 10;
int st[N / 4 + 10];
int cnt;
struct node
{
	int v;
	int id;
}S[N];
int n;
int cmp(node A, node B)
{
	return A.v < B.v;
}
bool is_ok()
{
	int cnt1 = 0;
	for (int i = 1; i <= n; i++) {
		cnt1 += (st[i] >= 1);
	}
	return cnt1 == n;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)st[i] = 0;
		cnt = 0;
		for (int i = 1; i <= n; i++) {
			int x;
			scanf("%d", &x);
			while (x--) {
				int y;
				scanf("%d", &y);
				S[++cnt] = { y,i };
			}
		}
		sort(S + 1, S + cnt + 1, cmp);
		int j = 1;
		int ans = 2e9;
		int cnt1 = 0;
		for (int i = 1; i <= cnt; i++) {
			int x = S[i].id;
			st[x]++;
			if (st[x] == 1)cnt1++;
			while (st[S[j].id] > 1) {
				st[S[j].id]--;
				j++;
			}
			//cout<<"cnt1=="<<cnt1<<endl;
			if (cnt1 == n) {
				ans = min(ans, S[i].v - S[j].v);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
